package com.yt.product.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.enums.CodeEnum;
import com.yt.common.exception.BusinessException;
import com.yt.common.utils.StrUtils;
import com.yt.product.models.bo.ProductInfoBO;
import com.yt.product.models.service.IProductCategoryService;
import com.yt.product.models.vo.ProductInfoVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.product.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.product.mapper.ProductInfoMapper;
import com.yt.product.models.service.IProductInfoService;
import com.yt.product.models.bo.ProductInfoSaveBO;
import com.yt.product.models.dto.ProductInfoDTO;

import java.util.Date;
import java.util.List;

/**
 * 产品信息控制器
 *
 * @author chen
 * @Date 2025-09-05 14:09:50
 */
@DubboService
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfoDTO> implements IProductInfoService {

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IProductCategoryService productCategoryService;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    public boolean add(ProductInfoSaveBO saveBO) {
        // 参数校验
        validateSaveBO(saveBO);

        ProductInfoDTO dto = new ProductInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);

        // 设置默认值
        setDefaultValues(dto);

        // 生成ID
        dto.setId(ymlConfig.getPkId());
        return this.insert(dto);
    }

    // 参数校验逻辑
    private void validateSaveBO(ProductInfoSaveBO saveBO) {
        if (saveBO == null) {
            throw new BusinessException(CodeEnum.FAIL_100001.getCode(), "产品信息不能为空");
        }
        if (StrUtils.isEmpty(saveBO.getProductName())) {
            throw new BusinessException(CodeEnum.FAIL_100002.getCode(), "产品名称不能为空");
        }
        if (saveBO.getCategoryId() == null) {
            throw new BusinessException(CodeEnum.FAIL_100003.getCode(), "产品类型ID不能为空");
        }
        // 校验产品类型是否存在
        if (productCategoryService.selectById(saveBO.getCategoryId()) == null) {
            throw new BusinessException(CodeEnum.FAIL_100010.getCode(), "产品类型不存在");
        }
        if (saveBO.getStatus() != null && (saveBO.getStatus() < 0 || saveBO.getStatus() > 1)) {
            throw new BusinessException(CodeEnum.FAIL_100004.getCode(), "状态必须为0（下架）或1（在售）");
        }
        if (saveBO.getStock() != null && saveBO.getStock() < 0) {
            throw new BusinessException(CodeEnum.FAIL_100006.getCode(), "库存数量不能为负数");
        }
    }

    // 设置默认值
    private void setDefaultValues(ProductInfoDTO dto) {
        dto.setRevision(dto.getRevision() != null ? dto.getRevision() : 0);
        dto.setStatus(dto.getStatus() != null ? dto.getStatus() : 1);
        dto.setStock(dto.getStock() != null ? dto.getStock() : 0);
        Date now = new Date();
        dto.setCreateTime(dto.getCreateTime() != null ? dto.getCreateTime() : now);
        dto.setUpdateTime(dto.getUpdateTime() != null ? dto.getUpdateTime() : now);
    }

    @Override
    public ProductInfoVO selectVoById(Long id) {
        return productInfoMapper.selectVoById(id);
    }

    @Override
    public Page<ProductInfoVO> selectVoPage(Page<ProductInfoVO> page, ProductInfoBO bo) {
        List<ProductInfoVO> records = productInfoMapper.selectVoPageList(page, bo);
        return page.setRecords(records);
    }

    @Override
    public List<ProductInfoVO> selectVoByProductName(String productName) {
        return productInfoMapper.selectVoByProductName(productName);
    }

    @Override
    public Page<ProductInfoVO> selectVoByCategoryNamePage(Page<ProductInfoVO> page, ProductInfoBO bo) {
        List<ProductInfoVO> records = productInfoMapper.selectVoByCategoryNamePage(page, bo);
        return page.setRecords(records);
    }
}